package com.dys.hejiayuncommunity.framework.service;

import com.dys.hejiayuncommunity.system.domain.SysUser;
import com.dys.hejiayuncommunity.system.service.SysMenuService;
import com.dys.hejiayuncommunity.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @className: com.dys.hejiayuncommunity.framework.service.SysPermissionService
 * @description: 获取用户的角色和菜单
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/12/23 21:26
 */
@Service
public class SysPermissionService {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 获取用户拥有的角色数据
     *
     * @param user 用户信息
     * @return 用户拥有的角色权限信息
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> rolePermission = new HashSet<>();
        // 管理员拥有所有的角色权限
        if (user.isAdmin()) {
            rolePermission.add("admin");
        } else {
            rolePermission = sysRoleService.selectRolePermissionByUserId(user.getUserId());
        }
        return rolePermission;
    }

    /**
     * 获取用户拥有的菜单数据
     *
     * @param user 用户信息
     * @return 用户拥有的菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> menuPermissions = new HashSet<>();
        // 管理员拥有所有的菜单权限
        if (user.isAdmin()) {
            menuPermissions.add("*:*:*");
        } else {
            menuPermissions = sysMenuService.selectMenuPermsByUserId(user.getUserId());
        }
        return menuPermissions;
    }
}
